﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Salon.Properties;
using System.IO;

namespace Salon
{
    public partial class MainPage : Form
    {
        public MainPage()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
            Global.Help = new HelpProvider();
            Global.Help.HelpNamespace = "salon.chm";
        }

        private void MainPage_Load(object sender, EventArgs e)
        {
            // Konfiguracja ConnectionString:
            try
            {
                FileStream fs = new FileStream("connection.txt", FileMode.Open, FileAccess.Read);
                try
                {
                    StreamReader sr = new StreamReader(fs);
                    sr.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Błąd odczytu \"connection.txt\" ");
                }
            }
            catch(Exception)
            {
                // wyświetlenie dialogu do konf połączenia
                FormConnectionConf conf_form = new FormConnectionConf();
                conf_form.ShowDialog();
                //MessageBox.Show("Brak pliku - tu bedzie konfiguracja");
            }

            //
            Login login = new Login(this);
            login.ShowDialog();
            this.Hide();
            Global.MainPage = this;

            
            Global.Help.SetHelpKeyword(this, "Okno główne programu");
            Global.Help.SetHelpNavigator(this, System.Windows.Forms.HelpNavigator.KeywordIndex);
            
            Global.Help.SetShowHelp(this, true);
            
            setSecurity();
        }

        /// <summary>
        /// Funkcja sprawdzająca czy formatka o danym typie jest już otwarta,
        /// jeśli jest to jest ona uwidaczniana
        /// </summary>
        /// <param name="formName"></param>
        public bool checkIfFormOpen(String formText)
        {
            foreach (Form form in this.MdiChildren)
            {
                if (form.Text.Equals(formText))
                {
                    form.Activate();
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Funkcja zweracająca czy formatkę o danej nazwie
        /// jeśli istnieje to jest ona zwracana 
        /// a jeśli nie to nie
        /// </summary>
        /// <param name="formName"></param>
        public Form getIfFormOpen(String formText)
        {
            foreach (Form form in this.MdiChildren)
            {
                if (form.Text.Equals(formText))
                {
                    return form;
                }
            }

            return null;
        }

       
        private void mojeDaneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.USER_MY_DATA))
            {
                FormUser fu = new FormUser(Global.currentUser,true);
                fu.MdiParent = this;     
                fu.Show();

            }
        }

        private void nowyUżytkownikToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            openNewUserForm(null,false);
        }

        public void openNewUserForm(User user,bool current)
        {
            if (!checkIfFormOpen(Forms.USER_FORM))
            {
                FormUser fu = new FormUser(user,current);
                fu.MdiParent = this;
                fu.Show();
            }
        }

        private void rejestrUżytkownikówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.USER_REGISTER))
            {
                RegisterUsers ru = new RegisterUsers();
                ru.MdiParent = this;
                ru.Text = Forms.USER_REGISTER;
                ru.Show();
            }
        }

        private void nowyKlientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openNewClientForm(null);
        }

        public void openNewClientForm(Client client)
        {
            if (!checkIfFormOpen(Forms.CLIENT_FORM))
            {
                FormClient fc = new FormClient(client);
                fc.MdiParent = this;
                fc.Show();
            }
        }

        private void rejestrKlientówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.CLIENT_REGISTER))
            {
                RegisterClients rc = new RegisterClients();
                rc.MdiParent = this;
                rc.Text = Forms.CLIENT_REGISTER;
                rc.Show();
            }
        }

        private void nowySalonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openNewAutoSalonForm(null);

        }

        public void openNewAutoSalonForm(AutoSalon salon)
        {
            if (!checkIfFormOpen(Forms.AUTOSALON_FORM))
            {
                FormAutoSalon fc = new FormAutoSalon(salon);
                fc.MdiParent = this;
                fc.Show();
            }
        }

        private void rejestrSalonówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.AUTOSALON_REGISTER))
            {
                RegisterAutoSalons rc = new RegisterAutoSalons();
                rc.MdiParent = this;
                rc.Text = Forms.AUTOSALON_REGISTER;
                rc.Show();
            }
        }

        private void rejestrSamochodówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.CAR_REGISTER))
            {
                RegisterCars car = new RegisterCars();
                car.MdiParent = this;
                car.Text = Forms.CAR_REGISTER;
                car.Show();
            }
        }

        private void nowySamochódToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openNewCarForm(null);
        }

        public void openNewCarForm(Car car)
        {
            if (!checkIfFormOpen(Forms.CAR_FORM))
            {
                FormCar fc = new FormCar(car);
                fc.MdiParent = this;
                fc.Show();
            }
        }

        private void nowyTransferToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openNewTransferForm(null);
        }

        public void openNewTransferForm(Transfer trans)
        {
            if (!checkIfFormOpen(Forms.TRANSFER_FORM))
            {
                FormTransfer ft = new FormTransfer(trans);
                ft.MdiParent = this;
                ft.Text = Forms.TRANSFER_FORM;
                ft.Show();
            }
        }

        private void rejestrTransferówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.TRANSFER_REGISTER))
            {
                RegisterTransfer transreg = new RegisterTransfer();
                transreg.MdiParent = this;
                transreg.Text = Forms.TRANSFER_REGISTER;
                transreg.Show();
            }
        }

        private void markiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.DICT_BRAND))
            {
                DictBrand brand = new DictBrand();
                brand.MdiParent = this;
                brand.Text = Forms.DICT_BRAND;
                brand.Show();
            }
        }

        private void modeleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.DICT_MODEL))
            {
                DictModel model = new DictModel();
                model.MdiParent = this;
                model.Text = Forms.DICT_MODEL;
                model.Show();
            }
        }

        private void opcjeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.DICT_OPTION))
            {
                DictOptions option = new DictOptions();
                option.MdiParent = this;
                option.Text = Forms.DICT_OPTION;
                option.Show();
            }
        }

        private void koloryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.DICT_COLORS))
            {
                DictColor color = new DictColor();
                color.MdiParent = this;
                color.Text = Forms.DICT_COLORS;
                color.Show();
            }
        }

        private void samochodyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.REPORT_CARS))
            {
                ReportCars form = new ReportCars();
                form.MdiParent = this;
                form.Text = Forms.REPORT_CARS;
                form.Show();
            }
        }

        private void noweZamówienieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openNewOrderForm(null);
        }

        public void openNewOrderForm(Order order)
        {
            if (!checkIfFormOpen(Forms.NEW_ORDER_FORM))
            {
                FormOrder form = new FormOrder(order);
                form.MdiParent = this;
                form.Text = Forms.NEW_ORDER_FORM;
                form.Show();
            }
        }



        private void rejestrZamówieńToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.ORDER_REGISTER))
            {
                RegisterOrders transreg = new RegisterOrders();
                transreg.MdiParent = this;
                transreg.Text = Forms.ORDER_REGISTER;
                transreg.Show();
            }
        }


        private void sprzedawcyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.REPORT_SALESMANS))
            {
                ReportSalesman form = new ReportSalesman();
                form.MdiParent = this;
                form.Text = Forms.REPORT_SALESMANS;
                form.Show();
            }
        }
        /// <summary>
        /// Funkcja ukrywająca opcje menu w zaleźnosci od ról aktualnego uzytkownika
        /// </summary>
        private void setSecurity()
        {
            nowyUżytkownikToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager);
            raportyToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager);
            //zamówieniaToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager | Roles.Salesman );
            słownikiToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator );
            klienciToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager | Roles.Salesman);
            salonyToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager | Roles.Salesman);
            rejestrUżytkownikówToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager);
            nowySalonToolStripMenuItem.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager);
            nowySamochódToolStripMenuItem1.Enabled = Global.checkRole(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager);
        }
            
    }
}
